import * as R from 'ramda';
import { version as uuidVersion } from 'uuid';
import uuidTime from 'uuid-time';
import {
  ENTITY_TYPE_ATTACK_PATTERN,
  ENTITY_TYPE_CAMPAIGN,
  ENTITY_TYPE_CONTAINER_OBSERVED_DATA,
  ENTITY_TYPE_COURSE_OF_ACTION,
  ENTITY_TYPE_IDENTITY_INDIVIDUAL,
  ENTITY_TYPE_IDENTITY_SECTOR,
  ENTITY_TYPE_IDENTITY_SYSTEM,
  ENTITY_TYPE_INCIDENT,
  ENTITY_TYPE_INFRASTRUCTURE,
  ENTITY_TYPE_INTRUSION_SET,
  ENTITY_TYPE_LOCATION_CITY,
  ENTITY_TYPE_LOCATION_COUNTRY,
  ENTITY_TYPE_LOCATION_POSITION,
  ENTITY_TYPE_LOCATION_REGION,
  ENTITY_TYPE_MALWARE,
  ENTITY_TYPE_THREAT_ACTOR_GROUP,
  ENTITY_TYPE_TOOL,
  ENTITY_TYPE_VULNERABILITY,
} from '../schema/stixDomainObject';
import {
  ENTITY_AUTONOMOUS_SYSTEM,
  ENTITY_DOMAIN_NAME,
  ENTITY_HASHED_OBSERVABLE_ARTIFACT,
  ENTITY_HASHED_OBSERVABLE_STIX_FILE,
  ENTITY_HOSTNAME,
  ENTITY_IPV4_ADDR,
  ENTITY_IPV6_ADDR,
  ENTITY_MAC_ADDR,
  ENTITY_MEDIA_CONTENT,
  ENTITY_SOFTWARE,
  ENTITY_TEXT,
  ENTITY_URL,
  ENTITY_USER_ACCOUNT,
  isStixCyberObservable
} from '../schema/stixCyberObservable';
import {
  RELATION_ATTRIBUTED_TO,
  RELATION_AUTHORED_BY,
  RELATION_BASED_ON,
  RELATION_BEACONS_TO,
  RELATION_BELONGS_TO,
  RELATION_COMMUNICATES_WITH,
  RELATION_COMPROMISES,
  RELATION_CONSISTS_OF,
  RELATION_CONTROLS,
  RELATION_COOPERATES_WITH,
  RELATION_DELIVERS,
  RELATION_DERIVED_FROM,
  RELATION_DETECTS,
  RELATION_DOWNLOADS,
  RELATION_DROPS,
  RELATION_EXFILTRATES_TO,
  RELATION_EXPLOITS,
  RELATION_HAS,
  RELATION_HOSTS,
  RELATION_IMPERSONATES,
  RELATION_INDICATES,
  RELATION_INVESTIGATES,
  RELATION_LOCATED_AT,
  RELATION_MITIGATES,
  RELATION_ORIGINATES_FROM,
  RELATION_OWNS,
  RELATION_PART_OF,
  RELATION_PARTICIPATES_IN,
  RELATION_PUBLISHES,
  RELATION_RELATED_TO,
  RELATION_REMEDIATES,
  RELATION_RESOLVES_TO,
  RELATION_REVOKED_BY,
  RELATION_SUBTECHNIQUE_OF,
  RELATION_TARGETS,
  RELATION_USES,
  RELATION_VARIANT_OF
} from '../schema/stixCoreRelationship';
import { isStixRefRelationship } from '../schema/stixRefRelationship';
import { ABSTRACT_STIX_CYBER_OBSERVABLE, ENTITY_TYPE_THREAT_ACTOR } from '../schema/general';
import { ENTITY_TYPE_EVENT } from '../modules/event/event-types';
import { ENTITY_TYPE_NARRATIVE } from '../modules/narrative/narrative-types';
import { ENTITY_TYPE_LOCATION_ADMINISTRATIVE_AREA } from '../modules/administrativeArea/administrativeArea-types';
import type { StoreRelation } from '../types/store';
import { isInternalRelationship } from '../schema/internalRelationship';
import { schemaRelationsRefDefinition } from '../schema/schema-relationsRef';
import { ENTITY_TYPE_CHANNEL } from '../modules/channel/channel-types';
import { FunctionalError, UnsupportedError } from '../config/errors';
import { ENTITY_TYPE_THREAT_ACTOR_INDIVIDUAL } from '../modules/threatActorIndividual/threatActorIndividual-types';
import { ENTITY_TYPE_IDENTITY_ORGANIZATION } from '../modules/organization/organization-types';
import { ENTITY_TYPE_INDICATOR } from '../modules/indicator/indicator-types';

const MAX_TRANSIENT_STIX_IDS = 200;
export const STIX_SPEC_VERSION = '2.1';

export const onlyStableStixIds = (ids = []) => R.filter((n) => uuidVersion(R.split('--', n)[1]) !== 1, ids);

export const cleanStixIds = (ids: Array<string>, maxStixIds = MAX_TRANSIENT_STIX_IDS): Array<string> => {
  const keptIds = [];
  const transientIds = [];
  const wIds = Array.isArray(ids) ? ids : [ids];
  for (let index = 0; index < wIds.length; index += 1) {
    const stixId = wIds[index];
    const segments = stixId.split('--');
    const [, uuid] = segments;
    const isTransient = uuidVersion(uuid) === 1;
    if (isTransient) {
      const timestamp = uuidTime.v1(uuid);
      transientIds.push({ id: stixId, uuid, timestamp });
    } else {
      keptIds.push({ id: stixId, uuid });
    }
  }
  const orderedTransient = R.sort((a, b) => b.timestamp - a.timestamp, transientIds);
  const keptTimedIds = orderedTransient.length > maxStixIds ? orderedTransient.slice(0, maxStixIds) : orderedTransient;
  // Return the new list
  return R.map((s) => s.id, [...keptIds, ...keptTimedIds]);
};

type RelationType = 'builtin' | 'new' | 'extended';
export const REL_BUILT_IN: RelationType = 'builtin';
export const REL_NEW: RelationType = 'new';
export const REL_EXTENDED: RelationType = 'extended';
export type RelationDefinition = { name: string; type: RelationType };
export type RelationshipMappings = { [k: `${string}_${string}`]: Array<RelationDefinition>; };

export const stixCoreRelationshipsMapping: RelationshipMappings = {
  // Core
  // region ATTACK_PATTERN
  [`${ENTITY_TYPE_ATTACK_PATTERN}_${ENTITY_TYPE_ATTACK_PATTERN}`]: [
    { name: RELATION_SUBTECHNIQUE_OF, type: REL_NEW }
  ],
  [`${ENTITY_TYPE_ATTACK_PATTERN}_${ENTITY_TYPE_IDENTITY_INDIVIDUAL}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_ATTACK_PATTERN}_${ENTITY_TYPE_IDENTITY_ORGANIZATION}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_ATTACK_PATTERN}_${ENTITY_TYPE_IDENTITY_SECTOR}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_ATTACK_PATTERN}_${ENTITY_TYPE_IDENTITY_SYSTEM}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_ATTACK_PATTERN}_${ENTITY_TYPE_EVENT}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_ATTACK_PATTERN}_${ENTITY_TYPE_LOCATION_POSITION}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_ATTACK_PATTERN}_${ENTITY_TYPE_LOCATION_CITY}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_ATTACK_PATTERN}_${ENTITY_TYPE_LOCATION_ADMINISTRATIVE_AREA}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_ATTACK_PATTERN}_${ENTITY_TYPE_LOCATION_COUNTRY}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_ATTACK_PATTERN}_${ENTITY_TYPE_LOCATION_REGION}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_ATTACK_PATTERN}_${ENTITY_TYPE_MALWARE}`]: [
    { name: RELATION_DELIVERS, type: REL_BUILT_IN },
    { name: RELATION_USES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_ATTACK_PATTERN}_${ENTITY_TYPE_TOOL}`]: [
    { name: RELATION_USES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_ATTACK_PATTERN}_${ENTITY_TYPE_VULNERABILITY}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  // endregion
  // region CAMPAIGN
  [`${ENTITY_TYPE_CAMPAIGN}_${ENTITY_TYPE_ATTACK_PATTERN}`]: [
    { name: RELATION_USES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_CAMPAIGN}_${ENTITY_TYPE_CHANNEL}`]: [
    { name: RELATION_USES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_CAMPAIGN}_${ENTITY_TYPE_NARRATIVE}`]: [
    { name: RELATION_USES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_CAMPAIGN}_${ENTITY_TYPE_EVENT}`]: [
    { name: RELATION_TARGETS, type: REL_EXTENDED }
  ],
  [`${ENTITY_TYPE_CAMPAIGN}_${ENTITY_TYPE_IDENTITY_INDIVIDUAL}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_CAMPAIGN}_${ENTITY_TYPE_IDENTITY_ORGANIZATION}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_CAMPAIGN}_${ENTITY_TYPE_IDENTITY_SECTOR}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_CAMPAIGN}_${ENTITY_TYPE_IDENTITY_SYSTEM}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_CAMPAIGN}_${ENTITY_TYPE_INFRASTRUCTURE}`]: [
    { name: RELATION_COMPROMISES, type: REL_BUILT_IN },
    { name: RELATION_USES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_CAMPAIGN}_${ENTITY_TYPE_INTRUSION_SET}`]: [
    { name: RELATION_ATTRIBUTED_TO, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_CAMPAIGN}_${ENTITY_TYPE_LOCATION_POSITION}`]: [
    { name: RELATION_ORIGINATES_FROM, type: REL_BUILT_IN },
    { name: RELATION_TARGETS, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_CAMPAIGN}_${ENTITY_TYPE_LOCATION_CITY}`]: [
    { name: RELATION_ORIGINATES_FROM, type: REL_BUILT_IN },
    { name: RELATION_TARGETS, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_CAMPAIGN}_${ENTITY_TYPE_LOCATION_ADMINISTRATIVE_AREA}`]: [
    { name: RELATION_ORIGINATES_FROM, type: REL_BUILT_IN },
    { name: RELATION_TARGETS, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_CAMPAIGN}_${ENTITY_TYPE_LOCATION_COUNTRY}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN },
    { name: RELATION_ORIGINATES_FROM, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_CAMPAIGN}_${ENTITY_TYPE_LOCATION_REGION}`]: [
    { name: RELATION_ORIGINATES_FROM, type: REL_BUILT_IN },
    { name: RELATION_TARGETS, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_CAMPAIGN}_${ENTITY_TYPE_MALWARE}`]: [
    { name: RELATION_USES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_CAMPAIGN}_${ENTITY_TYPE_THREAT_ACTOR}`]: [
    { name: RELATION_ATTRIBUTED_TO, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_CAMPAIGN}_${ENTITY_TYPE_THREAT_ACTOR_GROUP}`]: [
    { name: RELATION_ATTRIBUTED_TO, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_CAMPAIGN}_${ENTITY_TYPE_THREAT_ACTOR_INDIVIDUAL}`]: [
    { name: RELATION_ATTRIBUTED_TO, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_CAMPAIGN}_${ENTITY_TYPE_TOOL}`]: [
    { name: RELATION_USES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_CAMPAIGN}_${ENTITY_TYPE_VULNERABILITY}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  // endregion
  // region COURSE_OF_ACTION
  [`${ENTITY_TYPE_COURSE_OF_ACTION}_${ENTITY_TYPE_ATTACK_PATTERN}`]: [
    { name: RELATION_MITIGATES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_COURSE_OF_ACTION}_${ENTITY_TYPE_INDICATOR}`]: [
    { name: RELATION_INVESTIGATES, type: REL_BUILT_IN },
    { name: RELATION_MITIGATES, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_COURSE_OF_ACTION}_${ENTITY_TYPE_MALWARE}`]: [
    { name: RELATION_MITIGATES, type: REL_BUILT_IN },
    { name: RELATION_REMEDIATES, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_COURSE_OF_ACTION}_${ENTITY_TYPE_TOOL}`]: [
    { name: RELATION_MITIGATES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_COURSE_OF_ACTION}_${ENTITY_TYPE_VULNERABILITY}`]: [
    { name: RELATION_MITIGATES, type: REL_BUILT_IN },
    { name: RELATION_REMEDIATES, type: REL_BUILT_IN },
  ],
  // endregion
  // region INDIVIDUAL
  [`${ENTITY_TYPE_IDENTITY_INDIVIDUAL}_${ENTITY_TYPE_IDENTITY_INDIVIDUAL}`]: [
    { name: RELATION_PART_OF, type: REL_NEW }
  ],
  [`${ENTITY_TYPE_IDENTITY_INDIVIDUAL}_${ENTITY_TYPE_IDENTITY_ORGANIZATION}`]: [
    { name: RELATION_PART_OF, type: REL_NEW }
  ],
  [`${ENTITY_TYPE_IDENTITY_INDIVIDUAL}_${ENTITY_TYPE_LOCATION_POSITION}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_IDENTITY_INDIVIDUAL}_${ENTITY_TYPE_LOCATION_CITY}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_IDENTITY_INDIVIDUAL}_${ENTITY_TYPE_LOCATION_ADMINISTRATIVE_AREA}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_IDENTITY_INDIVIDUAL}_${ENTITY_TYPE_LOCATION_COUNTRY}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_IDENTITY_INDIVIDUAL}_${ENTITY_TYPE_LOCATION_REGION}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_IDENTITY_INDIVIDUAL}_${ENTITY_MEDIA_CONTENT}`]: [
    { name: RELATION_PUBLISHES, type: REL_NEW }
  ],
  // endregion
  // region SECTOR
  [`${ENTITY_TYPE_IDENTITY_SECTOR}_${ENTITY_TYPE_IDENTITY_SECTOR}`]: [
    { name: RELATION_PART_OF, type: REL_NEW }
  ],
  [`${ENTITY_TYPE_IDENTITY_SECTOR}_${ENTITY_TYPE_LOCATION_POSITION}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_IDENTITY_SECTOR}_${ENTITY_TYPE_LOCATION_CITY}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_IDENTITY_SECTOR}_${ENTITY_TYPE_LOCATION_ADMINISTRATIVE_AREA}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_IDENTITY_SECTOR}_${ENTITY_TYPE_LOCATION_COUNTRY}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_IDENTITY_SECTOR}_${ENTITY_TYPE_LOCATION_REGION}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN }
  ],
  // endregion
  // region SYSTEM
  [`${ENTITY_TYPE_IDENTITY_SYSTEM}_${ENTITY_TYPE_IDENTITY_ORGANIZATION}`]: [
    { name: RELATION_BELONGS_TO, type: REL_EXTENDED }
  ],
  [`${ENTITY_TYPE_IDENTITY_SYSTEM}_${ENTITY_TYPE_LOCATION_POSITION}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_IDENTITY_SYSTEM}_${ENTITY_TYPE_LOCATION_CITY}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_IDENTITY_SYSTEM}_${ENTITY_TYPE_LOCATION_ADMINISTRATIVE_AREA}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_IDENTITY_SYSTEM}_${ENTITY_TYPE_LOCATION_COUNTRY}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_IDENTITY_SYSTEM}_${ENTITY_TYPE_LOCATION_REGION}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN }
  ],
  // endregion
  // region INCIDENT
  [`${ENTITY_TYPE_INCIDENT}_${ENTITY_TYPE_ATTACK_PATTERN}`]: [
    { name: RELATION_USES, type: REL_EXTENDED }
  ],
  [`${ENTITY_TYPE_INCIDENT}_${ENTITY_TYPE_CHANNEL}`]: [
    { name: RELATION_USES, type: REL_EXTENDED }
  ],
  [`${ENTITY_TYPE_INCIDENT}_${ENTITY_TYPE_NARRATIVE}`]: [
    { name: RELATION_USES, type: REL_EXTENDED }
  ],
  [`${ENTITY_TYPE_INCIDENT}_${ENTITY_TYPE_CAMPAIGN}`]: [
    { name: RELATION_ATTRIBUTED_TO, type: REL_EXTENDED }
  ],
  [`${ENTITY_TYPE_INCIDENT}_${ENTITY_TYPE_IDENTITY_INDIVIDUAL}`]: [
    { name: RELATION_TARGETS, type: REL_EXTENDED }
  ],
  [`${ENTITY_TYPE_INCIDENT}_${ENTITY_TYPE_IDENTITY_ORGANIZATION}`]: [
    { name: RELATION_TARGETS, type: REL_EXTENDED }
  ],
  [`${ENTITY_TYPE_INCIDENT}_${ENTITY_TYPE_IDENTITY_SECTOR}`]: [
    { name: RELATION_TARGETS, type: REL_EXTENDED }
  ],
  [`${ENTITY_TYPE_INCIDENT}_${ENTITY_TYPE_IDENTITY_SYSTEM}`]: [
    { name: RELATION_TARGETS, type: REL_EXTENDED }
  ],
  [`${ENTITY_TYPE_INCIDENT}_${ENTITY_TYPE_EVENT}`]: [
    { name: RELATION_TARGETS, type: REL_EXTENDED }
  ],
  [`${ENTITY_TYPE_INCIDENT}_${ENTITY_TYPE_INFRASTRUCTURE}`]: [
    { name: RELATION_COMPROMISES, type: REL_EXTENDED },
    { name: RELATION_USES, type: REL_EXTENDED },
  ],
  [`${ENTITY_TYPE_INCIDENT}_${ENTITY_TYPE_INTRUSION_SET}`]: [
    { name: RELATION_ATTRIBUTED_TO, type: REL_EXTENDED }
  ],
  [`${ENTITY_TYPE_INCIDENT}_${ENTITY_TYPE_LOCATION_POSITION}`]: [
    { name: RELATION_ORIGINATES_FROM, type: REL_EXTENDED },
    { name: RELATION_TARGETS, type: REL_EXTENDED },
  ],
  [`${ENTITY_TYPE_INCIDENT}_${ENTITY_TYPE_LOCATION_CITY}`]: [
    { name: RELATION_ORIGINATES_FROM, type: REL_EXTENDED },
    { name: RELATION_TARGETS, type: REL_EXTENDED },
  ],
  [`${ENTITY_TYPE_INCIDENT}_${ENTITY_TYPE_LOCATION_ADMINISTRATIVE_AREA}`]: [
    { name: RELATION_ORIGINATES_FROM, type: REL_EXTENDED },
    { name: RELATION_TARGETS, type: REL_EXTENDED },
  ],
  [`${ENTITY_TYPE_INCIDENT}_${ENTITY_TYPE_LOCATION_COUNTRY}`]: [
    { name: RELATION_ORIGINATES_FROM, type: REL_EXTENDED },
    { name: RELATION_TARGETS, type: REL_EXTENDED },
  ],
  [`${ENTITY_TYPE_INCIDENT}_${ENTITY_TYPE_LOCATION_REGION}`]: [
    { name: RELATION_ORIGINATES_FROM, type: REL_EXTENDED },
    { name: RELATION_TARGETS, type: REL_EXTENDED },
  ],
  [`${ENTITY_TYPE_INCIDENT}_${ENTITY_TYPE_MALWARE}`]: [
    { name: RELATION_USES, type: REL_EXTENDED }
  ],
  [`${ENTITY_TYPE_INCIDENT}_${ENTITY_TYPE_THREAT_ACTOR}`]: [
    { name: RELATION_ATTRIBUTED_TO, type: REL_EXTENDED }
  ],
  [`${ENTITY_TYPE_INCIDENT}_${ENTITY_TYPE_THREAT_ACTOR_GROUP}`]: [
    { name: RELATION_ATTRIBUTED_TO, type: REL_EXTENDED }
  ],
  [`${ENTITY_TYPE_INCIDENT}_${ENTITY_TYPE_THREAT_ACTOR_INDIVIDUAL}`]: [
    { name: RELATION_ATTRIBUTED_TO, type: REL_EXTENDED }
  ],
  [`${ENTITY_TYPE_INCIDENT}_${ENTITY_TYPE_TOOL}`]: [
    { name: RELATION_USES, type: REL_EXTENDED }
  ],
  [`${ENTITY_TYPE_INCIDENT}_${ENTITY_TYPE_VULNERABILITY}`]: [
    { name: RELATION_TARGETS, type: REL_EXTENDED }
  ],
  // endregion
  // region INDICATOR
  [`${ENTITY_TYPE_INDICATOR}_${ABSTRACT_STIX_CYBER_OBSERVABLE}`]: [
    { name: RELATION_BASED_ON, type: REL_EXTENDED }
  ],
  [`${ENTITY_TYPE_INDICATOR}_${ENTITY_TYPE_ATTACK_PATTERN}`]: [
    { name: RELATION_INDICATES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INDICATOR}_${ENTITY_TYPE_CAMPAIGN}`]: [
    { name: RELATION_INDICATES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INDICATOR}_${ENTITY_TYPE_CONTAINER_OBSERVED_DATA}`]: [
    { name: RELATION_BASED_ON, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INDICATOR}_${ENTITY_TYPE_INCIDENT}`]: [
    { name: RELATION_INDICATES, type: REL_EXTENDED }
  ],
  [`${ENTITY_TYPE_INDICATOR}_${ENTITY_TYPE_INDICATOR}`]: [
    { name: RELATION_DERIVED_FROM, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INDICATOR}_${ENTITY_TYPE_INFRASTRUCTURE}`]: [
    { name: RELATION_INDICATES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INDICATOR}_${ENTITY_TYPE_INTRUSION_SET}`]: [
    { name: RELATION_INDICATES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INDICATOR}_${ENTITY_TYPE_MALWARE}`]: [
    { name: RELATION_INDICATES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INDICATOR}_${ENTITY_TYPE_THREAT_ACTOR}`]: [
    { name: RELATION_INDICATES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INDICATOR}_${ENTITY_TYPE_THREAT_ACTOR_GROUP}`]: [
    { name: RELATION_INDICATES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INDICATOR}_${ENTITY_TYPE_THREAT_ACTOR_INDIVIDUAL}`]: [
    { name: RELATION_INDICATES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INDICATOR}_${ENTITY_TYPE_TOOL}`]: [
    { name: RELATION_INDICATES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INDICATOR}_${ENTITY_TYPE_VULNERABILITY}`]: [
    { name: RELATION_INDICATES, type: REL_EXTENDED }
  ],
  // endregion
  // region INFRASTRUCTURE
  [`${ENTITY_TYPE_INFRASTRUCTURE}_${ABSTRACT_STIX_CYBER_OBSERVABLE}`]: [
    { name: RELATION_CONSISTS_OF, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INFRASTRUCTURE}_${ENTITY_DOMAIN_NAME}`]: [
    { name: RELATION_COMMUNICATES_WITH, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INFRASTRUCTURE}_${ENTITY_IPV4_ADDR}`]: [
    { name: RELATION_COMMUNICATES_WITH, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INFRASTRUCTURE}_${ENTITY_IPV6_ADDR}`]: [
    { name: RELATION_COMMUNICATES_WITH, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INFRASTRUCTURE}_${ENTITY_TYPE_CONTAINER_OBSERVED_DATA}`]: [
    { name: RELATION_CONSISTS_OF, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INFRASTRUCTURE}_${ENTITY_TYPE_INFRASTRUCTURE}`]: [
    { name: RELATION_COMMUNICATES_WITH, type: REL_BUILT_IN },
    { name: RELATION_CONSISTS_OF, type: REL_BUILT_IN },
    { name: RELATION_CONTROLS, type: REL_BUILT_IN },
    { name: RELATION_USES, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_INFRASTRUCTURE}_${ENTITY_TYPE_LOCATION_POSITION}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INFRASTRUCTURE}_${ENTITY_TYPE_LOCATION_CITY}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INFRASTRUCTURE}_${ENTITY_TYPE_LOCATION_ADMINISTRATIVE_AREA}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INFRASTRUCTURE}_${ENTITY_TYPE_LOCATION_COUNTRY}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INFRASTRUCTURE}_${ENTITY_TYPE_LOCATION_REGION}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INFRASTRUCTURE}_${ENTITY_TYPE_MALWARE}`]: [
    { name: RELATION_CONTROLS, type: REL_BUILT_IN },
    { name: RELATION_DELIVERS, type: REL_BUILT_IN },
    { name: RELATION_HOSTS, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_INFRASTRUCTURE}_${ENTITY_TYPE_TOOL}`]: [
    { name: RELATION_HOSTS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INFRASTRUCTURE}_${ENTITY_TYPE_VULNERABILITY}`]: [
    { name: RELATION_HAS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INFRASTRUCTURE}_${ENTITY_URL}`]: [
    { name: RELATION_COMMUNICATES_WITH, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INFRASTRUCTURE}_${ENTITY_SOFTWARE}`]: [
    { name: RELATION_CONSISTS_OF, type: REL_EXTENDED },
    { name: RELATION_HOSTS, type: REL_EXTENDED },
  ],
  [`${ENTITY_TYPE_INFRASTRUCTURE}_${ENTITY_TYPE_ATTACK_PATTERN}`]: [
    { name: RELATION_DETECTS, type: REL_NEW }
  ],
  // endregion
  // region INTRUSION_SET
  [`${ENTITY_TYPE_INTRUSION_SET}_${ENTITY_TYPE_ATTACK_PATTERN}`]: [
    { name: RELATION_USES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INTRUSION_SET}_${ENTITY_TYPE_CHANNEL}`]: [
    { name: RELATION_USES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INTRUSION_SET}_${ENTITY_TYPE_NARRATIVE}`]: [
    { name: RELATION_USES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INTRUSION_SET}_${ENTITY_TYPE_IDENTITY_INDIVIDUAL}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INTRUSION_SET}_${ENTITY_TYPE_IDENTITY_ORGANIZATION}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }],
  [`${ENTITY_TYPE_INTRUSION_SET}_${ENTITY_TYPE_IDENTITY_SECTOR}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INTRUSION_SET}_${ENTITY_TYPE_IDENTITY_SYSTEM}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INTRUSION_SET}_${ENTITY_TYPE_EVENT}`]: [
    { name: RELATION_TARGETS, type: REL_EXTENDED }
  ],
  [`${ENTITY_TYPE_INTRUSION_SET}_${ENTITY_TYPE_INFRASTRUCTURE}`]: [
    { name: RELATION_COMPROMISES, type: REL_BUILT_IN },
    { name: RELATION_HOSTS, type: REL_BUILT_IN },
    { name: RELATION_OWNS, type: REL_BUILT_IN },
    { name: RELATION_USES, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_INTRUSION_SET}_${ENTITY_TYPE_LOCATION_POSITION}`]: [
    { name: RELATION_ORIGINATES_FROM, type: REL_BUILT_IN },
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INTRUSION_SET}_${ENTITY_TYPE_LOCATION_CITY}`]: [
    { name: RELATION_ORIGINATES_FROM, type: REL_BUILT_IN },
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INTRUSION_SET}_${ENTITY_TYPE_LOCATION_ADMINISTRATIVE_AREA}`]: [
    { name: RELATION_ORIGINATES_FROM, type: REL_BUILT_IN },
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INTRUSION_SET}_${ENTITY_TYPE_LOCATION_COUNTRY}`]: [
    { name: RELATION_ORIGINATES_FROM, type: REL_BUILT_IN },
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INTRUSION_SET}_${ENTITY_TYPE_LOCATION_REGION}`]: [
    { name: RELATION_ORIGINATES_FROM, type: REL_BUILT_IN },
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INTRUSION_SET}_${ENTITY_TYPE_MALWARE}`]: [
    { name: RELATION_USES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INTRUSION_SET}_${ENTITY_TYPE_THREAT_ACTOR}`]: [
    { name: RELATION_ATTRIBUTED_TO, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INTRUSION_SET}_${ENTITY_TYPE_THREAT_ACTOR_INDIVIDUAL}`]: [
    { name: RELATION_ATTRIBUTED_TO, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INTRUSION_SET}_${ENTITY_TYPE_THREAT_ACTOR_GROUP}`]: [
    { name: RELATION_ATTRIBUTED_TO, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INTRUSION_SET}_${ENTITY_TYPE_CAMPAIGN}`]: [
    { name: RELATION_ATTRIBUTED_TO, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INTRUSION_SET}_${ENTITY_TYPE_TOOL}`]: [
    { name: RELATION_USES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INTRUSION_SET}_${ENTITY_TYPE_VULNERABILITY}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_INTRUSION_SET}_${ENTITY_HASHED_OBSERVABLE_STIX_FILE}`]: [
    { name: RELATION_USES, type: REL_EXTENDED }
  ],
  // endregion
  // region POSITION
  [`${ENTITY_TYPE_LOCATION_POSITION}_${ENTITY_TYPE_LOCATION_CITY}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_LOCATION_POSITION}_${ENTITY_TYPE_LOCATION_ADMINISTRATIVE_AREA}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_LOCATION_POSITION}_${ENTITY_TYPE_LOCATION_COUNTRY}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_LOCATION_POSITION}_${ENTITY_TYPE_LOCATION_REGION}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN }
  ],
  // endregion
  // region CITY
  [`${ENTITY_TYPE_LOCATION_CITY}_${ENTITY_TYPE_LOCATION_ADMINISTRATIVE_AREA}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_LOCATION_CITY}_${ENTITY_TYPE_LOCATION_COUNTRY}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_LOCATION_CITY}_${ENTITY_TYPE_LOCATION_REGION}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN }
  ],
  // endregion
  // region COUNTRY
  [`${ENTITY_TYPE_LOCATION_COUNTRY}_${ENTITY_TYPE_LOCATION_REGION}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN }
  ],
  // endregion
  // region LOCATION_REGION
  [`${ENTITY_TYPE_LOCATION_REGION}_${ENTITY_TYPE_LOCATION_REGION}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN }
  ],
  // endregion
  // region MALWARE
  [`${ENTITY_TYPE_MALWARE}_${ENTITY_DOMAIN_NAME}`]: [
    { name: RELATION_COMMUNICATES_WITH, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_MALWARE}_${ENTITY_HASHED_OBSERVABLE_STIX_FILE}`]: [
    { name: RELATION_DOWNLOADS, type: REL_BUILT_IN },
    { name: RELATION_DROPS, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_MALWARE}_${ENTITY_IPV4_ADDR}`]: [
    { name: RELATION_COMMUNICATES_WITH, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_MALWARE}_${ENTITY_IPV6_ADDR}`]: [
    { name: RELATION_COMMUNICATES_WITH, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_MALWARE}_${ENTITY_TYPE_ATTACK_PATTERN}`]: [
    { name: RELATION_USES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_MALWARE}_${ENTITY_TYPE_IDENTITY_INDIVIDUAL}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_MALWARE}_${ENTITY_TYPE_IDENTITY_ORGANIZATION}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_MALWARE}_${ENTITY_TYPE_IDENTITY_SECTOR}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_MALWARE}_${ENTITY_TYPE_IDENTITY_SYSTEM}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_MALWARE}_${ENTITY_TYPE_EVENT}`]: [
    { name: RELATION_TARGETS, type: REL_EXTENDED }
  ],
  [`${ENTITY_TYPE_MALWARE}_${ENTITY_TYPE_INFRASTRUCTURE}`]: [
    { name: RELATION_BEACONS_TO, type: REL_BUILT_IN },
    { name: RELATION_EXFILTRATES_TO, type: REL_BUILT_IN },
    { name: RELATION_TARGETS, type: REL_BUILT_IN },
    { name: RELATION_USES, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_MALWARE}_${ENTITY_TYPE_INTRUSION_SET}`]: [
    { name: RELATION_AUTHORED_BY, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_MALWARE}_${ENTITY_TYPE_LOCATION_POSITION}`]: [
    { name: RELATION_ORIGINATES_FROM, type: REL_BUILT_IN },
    { name: RELATION_TARGETS, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_MALWARE}_${ENTITY_TYPE_LOCATION_CITY}`]: [
    { name: RELATION_ORIGINATES_FROM, type: REL_BUILT_IN },
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_MALWARE}_${ENTITY_TYPE_LOCATION_ADMINISTRATIVE_AREA}`]: [
    { name: RELATION_ORIGINATES_FROM, type: REL_BUILT_IN },
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_MALWARE}_${ENTITY_TYPE_LOCATION_COUNTRY}`]: [
    { name: RELATION_ORIGINATES_FROM, type: REL_BUILT_IN },
    { name: RELATION_TARGETS, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_MALWARE}_${ENTITY_TYPE_LOCATION_REGION}`]: [
    { name: RELATION_ORIGINATES_FROM, type: REL_BUILT_IN },
    { name: RELATION_TARGETS, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_MALWARE}_${ENTITY_TYPE_MALWARE}`]: [
    { name: RELATION_CONTROLS, type: REL_BUILT_IN },
    { name: RELATION_DOWNLOADS, type: REL_BUILT_IN },
    { name: RELATION_DROPS, type: REL_BUILT_IN },
    { name: RELATION_USES, type: REL_BUILT_IN },
    { name: RELATION_VARIANT_OF, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_MALWARE}_${ENTITY_TYPE_THREAT_ACTOR}`]: [
    { name: RELATION_AUTHORED_BY, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_MALWARE}_${ENTITY_TYPE_THREAT_ACTOR_INDIVIDUAL}`]: [
    { name: RELATION_AUTHORED_BY, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_MALWARE}_${ENTITY_TYPE_THREAT_ACTOR_GROUP}`]: [
    { name: RELATION_AUTHORED_BY, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_MALWARE}_${ENTITY_TYPE_TOOL}`]: [
    { name: RELATION_DOWNLOADS, type: REL_BUILT_IN },
    { name: RELATION_DROPS, type: REL_BUILT_IN },
    { name: RELATION_USES, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_MALWARE}_${ENTITY_TYPE_VULNERABILITY}`]: [
    { name: RELATION_EXPLOITS, type: REL_BUILT_IN },
    { name: RELATION_TARGETS, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_MALWARE}_${ENTITY_URL}`]: [
    { name: RELATION_COMMUNICATES_WITH, type: REL_BUILT_IN }
  ],
  // endregion
  // region THREAT_ACTOR
  [`${ENTITY_TYPE_THREAT_ACTOR}_${ENTITY_TYPE_ATTACK_PATTERN}`]: [
    { name: RELATION_USES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR}_${ENTITY_TYPE_CHANNEL}`]: [
    { name: RELATION_USES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR}_${ENTITY_TYPE_NARRATIVE}`]: [
    { name: RELATION_USES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR}_${ENTITY_TYPE_CAMPAIGN}`]: [
    { name: RELATION_PARTICIPATES_IN, type: REL_NEW }
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR}_${ENTITY_TYPE_IDENTITY_INDIVIDUAL}`]: [
    { name: RELATION_ATTRIBUTED_TO, type: REL_BUILT_IN },
    { name: RELATION_IMPERSONATES, type: REL_BUILT_IN },
    { name: RELATION_TARGETS, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR}_${ENTITY_TYPE_IDENTITY_ORGANIZATION}`]: [
    { name: RELATION_ATTRIBUTED_TO, type: REL_BUILT_IN },
    { name: RELATION_IMPERSONATES, type: REL_BUILT_IN },
    { name: RELATION_TARGETS, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR}_${ENTITY_TYPE_IDENTITY_SECTOR}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR}_${ENTITY_TYPE_IDENTITY_SYSTEM}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR}_${ENTITY_TYPE_EVENT}`]: [
    { name: RELATION_TARGETS, type: REL_EXTENDED }
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR}_${ENTITY_TYPE_INFRASTRUCTURE}`]: [
    { name: RELATION_COMPROMISES, type: REL_BUILT_IN },
    { name: RELATION_HOSTS, type: REL_BUILT_IN },
    { name: RELATION_OWNS, type: REL_BUILT_IN },
    { name: RELATION_USES, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR}_${ENTITY_TYPE_LOCATION_POSITION}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN },
    { name: RELATION_TARGETS, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR}_${ENTITY_TYPE_LOCATION_CITY}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN },
    { name: RELATION_TARGETS, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR}_${ENTITY_TYPE_LOCATION_ADMINISTRATIVE_AREA}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN },
    { name: RELATION_TARGETS, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR}_${ENTITY_TYPE_LOCATION_COUNTRY}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN },
    { name: RELATION_TARGETS, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR}_${ENTITY_TYPE_LOCATION_REGION}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN },
    { name: RELATION_TARGETS, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR}_${ENTITY_TYPE_MALWARE}`]: [
    { name: RELATION_USES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR}_${ENTITY_TYPE_THREAT_ACTOR}`]: [
    { name: RELATION_PART_OF, type: REL_NEW },
    { name: RELATION_COOPERATES_WITH, type: REL_NEW },
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR}_${ENTITY_TYPE_THREAT_ACTOR_INDIVIDUAL}`]: [
    { name: RELATION_PART_OF, type: REL_NEW },
    { name: RELATION_COOPERATES_WITH, type: REL_NEW },
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR}_${ENTITY_TYPE_TOOL}`]: [
    { name: RELATION_USES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR}_${ENTITY_TYPE_VULNERABILITY}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR}_${ENTITY_HASHED_OBSERVABLE_STIX_FILE}`]: [
    { name: RELATION_USES, type: REL_EXTENDED }
  ],
  // endregion
  // region THREAT_ACTOR_GROUP
  [`${ENTITY_TYPE_THREAT_ACTOR_GROUP}_${ENTITY_TYPE_ATTACK_PATTERN}`]: [
    { name: RELATION_USES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR_GROUP}_${ENTITY_TYPE_CHANNEL}`]: [
    { name: RELATION_USES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR_GROUP}_${ENTITY_TYPE_NARRATIVE}`]: [
    { name: RELATION_USES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR_GROUP}_${ENTITY_TYPE_CAMPAIGN}`]: [
    { name: RELATION_PARTICIPATES_IN, type: REL_NEW }
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR_GROUP}_${ENTITY_TYPE_IDENTITY_INDIVIDUAL}`]: [
    { name: RELATION_ATTRIBUTED_TO, type: REL_BUILT_IN },
    { name: RELATION_IMPERSONATES, type: REL_BUILT_IN },
    { name: RELATION_TARGETS, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR_GROUP}_${ENTITY_TYPE_IDENTITY_ORGANIZATION}`]: [
    { name: RELATION_ATTRIBUTED_TO, type: REL_BUILT_IN },
    { name: RELATION_IMPERSONATES, type: REL_BUILT_IN },
    { name: RELATION_TARGETS, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR_GROUP}_${ENTITY_TYPE_IDENTITY_SECTOR}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR_GROUP}_${ENTITY_TYPE_IDENTITY_SYSTEM}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR_GROUP}_${ENTITY_TYPE_EVENT}`]: [
    { name: RELATION_TARGETS, type: REL_EXTENDED }
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR_GROUP}_${ENTITY_TYPE_INFRASTRUCTURE}`]: [
    { name: RELATION_COMPROMISES, type: REL_BUILT_IN },
    { name: RELATION_HOSTS, type: REL_BUILT_IN },
    { name: RELATION_OWNS, type: REL_BUILT_IN },
    { name: RELATION_USES, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR_GROUP}_${ENTITY_TYPE_LOCATION_POSITION}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN },
    { name: RELATION_TARGETS, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR_GROUP}_${ENTITY_TYPE_LOCATION_CITY}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN },
    { name: RELATION_TARGETS, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR_GROUP}_${ENTITY_TYPE_LOCATION_ADMINISTRATIVE_AREA}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN },
    { name: RELATION_TARGETS, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR_GROUP}_${ENTITY_TYPE_LOCATION_COUNTRY}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN },
    { name: RELATION_TARGETS, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR_GROUP}_${ENTITY_TYPE_LOCATION_REGION}`]: [
    { name: RELATION_LOCATED_AT, type: REL_BUILT_IN },
    { name: RELATION_TARGETS, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR_GROUP}_${ENTITY_TYPE_MALWARE}`]: [
    { name: RELATION_USES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR_GROUP}_${ENTITY_TYPE_THREAT_ACTOR_GROUP}`]: [
    { name: RELATION_PART_OF, type: REL_NEW },
    { name: RELATION_COOPERATES_WITH, type: REL_NEW },
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR_GROUP}_${ENTITY_TYPE_THREAT_ACTOR_INDIVIDUAL}`]: [
    { name: RELATION_PART_OF, type: REL_NEW },
    { name: RELATION_COOPERATES_WITH, type: REL_NEW },
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR_GROUP}_${ENTITY_TYPE_TOOL}`]: [
    { name: RELATION_USES, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR_GROUP}_${ENTITY_TYPE_VULNERABILITY}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_THREAT_ACTOR_GROUP}_${ENTITY_HASHED_OBSERVABLE_STIX_FILE}`]: [
    { name: RELATION_USES, type: REL_EXTENDED }
  ],
  // endregion
  // region TOOL
  [`${ENTITY_TYPE_TOOL}_${ENTITY_TYPE_IDENTITY_INDIVIDUAL}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_TOOL}_${ENTITY_TYPE_IDENTITY_ORGANIZATION}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_TOOL}_${ENTITY_TYPE_IDENTITY_SECTOR}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_TOOL}_${ENTITY_TYPE_EVENT}`]: [
    { name: RELATION_TARGETS, type: REL_EXTENDED }
  ],
  [`${ENTITY_TYPE_TOOL}_${ENTITY_TYPE_ATTACK_PATTERN}`]: [
    { name: RELATION_USES, type: REL_EXTENDED },
  ],
  [`${ENTITY_TYPE_TOOL}_${ENTITY_TYPE_INFRASTRUCTURE}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN },
    { name: RELATION_USES, type: REL_EXTENDED },
  ],
  [`${ENTITY_TYPE_TOOL}_${ENTITY_TYPE_LOCATION_POSITION}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_TOOL}_${ENTITY_TYPE_LOCATION_CITY}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_TOOL}_${ENTITY_TYPE_LOCATION_ADMINISTRATIVE_AREA}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_TOOL}_${ENTITY_TYPE_LOCATION_COUNTRY}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_TOOL}_${ENTITY_TYPE_LOCATION_REGION}`]: [
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  [`${ENTITY_TYPE_TOOL}_${ENTITY_TYPE_IDENTITY_SYSTEM}`]: [
    { name: RELATION_TARGETS, type: REL_EXTENDED }
  ],
  [`${ENTITY_TYPE_TOOL}_${ENTITY_TYPE_MALWARE}`]: [
    { name: RELATION_DELIVERS, type: REL_BUILT_IN },
    { name: RELATION_DROPS, type: REL_BUILT_IN },
  ],
  [`${ENTITY_TYPE_TOOL}_${ENTITY_TYPE_VULNERABILITY}`]: [
    { name: RELATION_HAS, type: REL_BUILT_IN },
    { name: RELATION_TARGETS, type: REL_BUILT_IN }
  ],
  // endregion
  // Observables
  // region HOSTNAME
  [`${ENTITY_HOSTNAME}_${ENTITY_HASHED_OBSERVABLE_ARTIFACT}`]: [
    { name: RELATION_DROPS, type: REL_EXTENDED }
  ],
  [`${ENTITY_HOSTNAME}_${ENTITY_TYPE_ATTACK_PATTERN}`]: [
    { name: RELATION_USES, type: REL_EXTENDED }
  ],
  [`${ENTITY_HOSTNAME}_${ENTITY_DOMAIN_NAME}`]: [
    { name: RELATION_COMMUNICATES_WITH, type: REL_EXTENDED }
  ],
  [`${ENTITY_HOSTNAME}_${ENTITY_IPV4_ADDR}`]: [
    { name: RELATION_COMMUNICATES_WITH, type: REL_EXTENDED },
    { name: RELATION_RESOLVES_TO, type: REL_EXTENDED }
  ],
  [`${ENTITY_HOSTNAME}_${ENTITY_IPV6_ADDR}`]: [
    { name: RELATION_COMMUNICATES_WITH, type: REL_EXTENDED },
    { name: RELATION_RESOLVES_TO, type: REL_EXTENDED }
  ],
  [`${ENTITY_HOSTNAME}_${ENTITY_HASHED_OBSERVABLE_STIX_FILE}`]: [
    { name: RELATION_DROPS, type: REL_EXTENDED }
  ],
  // endregion
  // region URL
  [`${ENTITY_URL}_${ENTITY_TYPE_NARRATIVE}`]: [
    { name: RELATION_USES, type: REL_EXTENDED }
  ],
  // endregion
  // region TEXT
  [`${ENTITY_TEXT}_${ENTITY_TYPE_NARRATIVE}`]: [
    { name: RELATION_USES, type: REL_EXTENDED }
  ],
  // endregion
  // region ARTIFACT
  [`${ENTITY_HASHED_OBSERVABLE_ARTIFACT}_${ENTITY_TYPE_NARRATIVE}`]: [
    { name: RELATION_USES, type: REL_EXTENDED }
  ],
  // endregion
  // region IPV4_ADDR
  [`${ENTITY_IPV4_ADDR}_${ENTITY_TYPE_LOCATION_POSITION}`]: [
    { name: RELATION_LOCATED_AT, type: REL_EXTENDED }
  ],
  [`${ENTITY_IPV4_ADDR}_${ENTITY_TYPE_LOCATION_CITY}`]: [
    { name: RELATION_LOCATED_AT, type: REL_EXTENDED }
  ],
  [`${ENTITY_IPV4_ADDR}_${ENTITY_TYPE_LOCATION_ADMINISTRATIVE_AREA}`]: [
    { name: RELATION_LOCATED_AT, type: REL_EXTENDED }
  ],
  [`${ENTITY_IPV4_ADDR}_${ENTITY_TYPE_LOCATION_COUNTRY}`]: [
    { name: RELATION_LOCATED_AT, type: REL_EXTENDED }
  ],
  [`${ENTITY_IPV4_ADDR}_${ENTITY_TYPE_LOCATION_REGION}`]: [
    { name: RELATION_LOCATED_AT, type: REL_EXTENDED }
  ],
  [`${ENTITY_IPV4_ADDR}_${ENTITY_MAC_ADDR}`]: [
    { name: RELATION_RESOLVES_TO, type: REL_BUILT_IN }
  ],
  [`${ENTITY_IPV4_ADDR}_${ENTITY_AUTONOMOUS_SYSTEM}`]: [
    { name: RELATION_BELONGS_TO, type: REL_BUILT_IN }
  ],
  [`${ENTITY_IPV4_ADDR}_${ENTITY_TYPE_IDENTITY_ORGANIZATION}`]: [
    { name: RELATION_BELONGS_TO, type: REL_EXTENDED }
  ],
  [`${ENTITY_IPV4_ADDR}_${ENTITY_TYPE_IDENTITY_INDIVIDUAL}`]: [
    { name: RELATION_BELONGS_TO, type: REL_EXTENDED }
  ],
  // endregion
  // region IPV6_ADDR
  [`${ENTITY_IPV6_ADDR}_${ENTITY_TYPE_LOCATION_POSITION}`]: [
    { name: RELATION_LOCATED_AT, type: REL_EXTENDED }
  ],
  [`${ENTITY_IPV6_ADDR}_${ENTITY_TYPE_LOCATION_CITY}`]: [
    { name: RELATION_LOCATED_AT, type: REL_EXTENDED }
  ],
  [`${ENTITY_IPV6_ADDR}_${ENTITY_TYPE_LOCATION_ADMINISTRATIVE_AREA}`]: [
    { name: RELATION_LOCATED_AT, type: REL_EXTENDED }
  ],
  [`${ENTITY_IPV6_ADDR}_${ENTITY_TYPE_LOCATION_COUNTRY}`]: [
    { name: RELATION_LOCATED_AT, type: REL_EXTENDED }
  ],
  [`${ENTITY_IPV6_ADDR}_${ENTITY_TYPE_LOCATION_REGION}`]: [
    { name: RELATION_LOCATED_AT, type: REL_EXTENDED }
  ],
  [`${ENTITY_IPV6_ADDR}_${ENTITY_MAC_ADDR}`]: [
    { name: RELATION_RESOLVES_TO, type: REL_BUILT_IN }
  ],
  [`${ENTITY_IPV6_ADDR}_${ENTITY_AUTONOMOUS_SYSTEM}`]: [
    { name: RELATION_BELONGS_TO, type: REL_BUILT_IN }
  ],
  [`${ENTITY_IPV6_ADDR}_${ENTITY_TYPE_IDENTITY_ORGANIZATION}`]: [
    { name: RELATION_BELONGS_TO, type: REL_EXTENDED }
  ],
  [`${ENTITY_IPV6_ADDR}_${ENTITY_TYPE_IDENTITY_INDIVIDUAL}`]: [
    { name: RELATION_BELONGS_TO, type: REL_EXTENDED }
  ],
  // endregion
  // region DOMAIN_NAME
  [`${ENTITY_DOMAIN_NAME}_${ENTITY_DOMAIN_NAME}`]: [
    { name: RELATION_RESOLVES_TO, type: REL_BUILT_IN }
  ],
  [`${ENTITY_DOMAIN_NAME}_${ENTITY_IPV4_ADDR}`]: [
    { name: RELATION_RESOLVES_TO, type: REL_BUILT_IN }
  ],
  [`${ENTITY_DOMAIN_NAME}_${ENTITY_IPV6_ADDR}`]: [
    { name: RELATION_RESOLVES_TO, type: REL_BUILT_IN }
  ],
  [`${ENTITY_DOMAIN_NAME}_${ENTITY_TYPE_NARRATIVE}`]: [
    { name: RELATION_USES, type: REL_EXTENDED }
  ],
  [`${ENTITY_DOMAIN_NAME}_${ENTITY_TYPE_IDENTITY_ORGANIZATION}`]: [
    { name: RELATION_BELONGS_TO, type: REL_EXTENDED }
  ],
  [`${ENTITY_DOMAIN_NAME}_${ENTITY_TYPE_IDENTITY_INDIVIDUAL}`]: [
    { name: RELATION_BELONGS_TO, type: REL_EXTENDED }
  ],
  // endregion
  // region STIX_FILE
  [`${ENTITY_HASHED_OBSERVABLE_STIX_FILE}_${ENTITY_TYPE_NARRATIVE}`]: [
    { name: RELATION_USES, type: REL_EXTENDED }
  ],
  [`${ENTITY_HASHED_OBSERVABLE_STIX_FILE}_${ENTITY_TYPE_ATTACK_PATTERN}`]: [
    { name: RELATION_USES, type: REL_EXTENDED }
  ],
  [`${ENTITY_HASHED_OBSERVABLE_STIX_FILE}_${ENTITY_HASHED_OBSERVABLE_STIX_FILE}`]: [
    { name: RELATION_DROPS, type: REL_EXTENDED },
    { name: RELATION_DOWNLOADS, type: REL_EXTENDED }
  ],
  [`${ENTITY_HASHED_OBSERVABLE_STIX_FILE}_${ENTITY_IPV4_ADDR}`]: [
    { name: RELATION_COMMUNICATES_WITH, type: REL_EXTENDED }
  ],
  [`${ENTITY_HASHED_OBSERVABLE_STIX_FILE}_${ENTITY_IPV6_ADDR}`]: [
    { name: RELATION_COMMUNICATES_WITH, type: REL_EXTENDED }
  ],
  [`${ENTITY_HASHED_OBSERVABLE_STIX_FILE}_${ENTITY_DOMAIN_NAME}`]: [
    { name: RELATION_COMMUNICATES_WITH, type: REL_EXTENDED }
  ],
  [`${ENTITY_HASHED_OBSERVABLE_STIX_FILE}_${ENTITY_TYPE_VULNERABILITY}`]: [
    { name: RELATION_TARGETS, type: REL_EXTENDED }
  ],
  // endregion
  // region ARTIFACT
  [`${ENTITY_HASHED_OBSERVABLE_ARTIFACT}_${ENTITY_IPV4_ADDR}`]: [
    { name: RELATION_COMMUNICATES_WITH, type: REL_EXTENDED }
  ],
  [`${ENTITY_HASHED_OBSERVABLE_ARTIFACT}_${ENTITY_IPV6_ADDR}`]: [
    { name: RELATION_COMMUNICATES_WITH, type: REL_EXTENDED }
  ],
  [`${ENTITY_HASHED_OBSERVABLE_ARTIFACT}_${ENTITY_DOMAIN_NAME}`]: [
    { name: RELATION_COMMUNICATES_WITH, type: REL_EXTENDED }
  ],
  [`${ENTITY_HASHED_OBSERVABLE_ARTIFACT}_${ENTITY_TYPE_ATTACK_PATTERN}`]: [
    { name: RELATION_USES, type: REL_EXTENDED }
  ],
  // endregion
  // region SOFTWARE
  [`${ENTITY_SOFTWARE}_${ENTITY_TYPE_VULNERABILITY}`]: [
    { name: RELATION_HAS, type: REL_EXTENDED }
  ],
  // From MEDIA CONTENT
  [`${ENTITY_MEDIA_CONTENT}_${ENTITY_TYPE_IDENTITY_INDIVIDUAL}`]: [
    { name: RELATION_AUTHORED_BY, type: REL_EXTENDED }
  ],
  [`${ENTITY_MEDIA_CONTENT}_${ENTITY_TYPE_IDENTITY_ORGANIZATION}`]: [
    { name: RELATION_AUTHORED_BY, type: REL_EXTENDED }
  ],
  [`${ENTITY_MEDIA_CONTENT}_${ENTITY_USER_ACCOUNT}`]: [
    { name: RELATION_AUTHORED_BY, type: REL_EXTENDED }
  ],
  // From USER_ACCOUNT
  [`${ENTITY_USER_ACCOUNT}_${ENTITY_MEDIA_CONTENT}`]: [
    { name: RELATION_PUBLISHES, type: REL_NEW }
  ],
  // endregion
  // Extended
  // region RELATIONS TO RELATIONS: DISCUSS IMPLEMENTATION!!
  [`${ENTITY_TYPE_INDICATOR}_${RELATION_USES}`]: [
    { name: RELATION_INDICATES, type: REL_EXTENDED }
  ],
  [`${RELATION_TARGETS}_${ENTITY_TYPE_LOCATION_POSITION}`]: [
    { name: RELATION_LOCATED_AT, type: REL_EXTENDED }
  ],
  [`${RELATION_TARGETS}_${ENTITY_TYPE_LOCATION_CITY}`]: [
    { name: RELATION_LOCATED_AT, type: REL_EXTENDED }
  ],
  [`${RELATION_TARGETS}_${ENTITY_TYPE_LOCATION_ADMINISTRATIVE_AREA}`]: [
    { name: RELATION_LOCATED_AT, type: REL_EXTENDED }
  ],
  [`${RELATION_TARGETS}_${ENTITY_TYPE_LOCATION_COUNTRY}`]: [
    { name: RELATION_LOCATED_AT, type: REL_EXTENDED }
  ],
  [`${RELATION_TARGETS}_${ENTITY_TYPE_LOCATION_REGION}`]: [
    { name: RELATION_LOCATED_AT, type: REL_EXTENDED }
  ],
  // endregion
};

export const checkStixCoreRelationshipMapping = (fromType: string, toType: string, relationshipType: string): boolean => {
  // RELATED_TO and REVOKED_BY are available for every entity
  if (relationshipType === RELATION_RELATED_TO || relationshipType === RELATION_REVOKED_BY) {
    return true;
  }
  // If core relationship start or target a cyber observable
  // All relationships here are a STIX specification extension.
  if (isStixCyberObservable(toType)) {
    const data = stixCoreRelationshipsMapping[`${fromType}_${ABSTRACT_STIX_CYBER_OBSERVABLE}`] ?? [];
    const mappingElements = data.map((r) => r.name);
    const haveKey = R.includes(`${fromType}_${ABSTRACT_STIX_CYBER_OBSERVABLE}`, R.keys(stixCoreRelationshipsMapping));
    const haveAccessibleTarget = R.includes(relationshipType, mappingElements);
    if (haveKey && haveAccessibleTarget) {
      return true;
    }
  }
  if (isStixCyberObservable(fromType)) {
    const data = stixCoreRelationshipsMapping[`${ABSTRACT_STIX_CYBER_OBSERVABLE}_${toType}`] ?? [];
    const mappingElements = data.map((r) => r.name);
    const haveKey = R.includes(`${ABSTRACT_STIX_CYBER_OBSERVABLE}_${toType}`, R.keys(stixCoreRelationshipsMapping));
    const haveAccessibleTarget = R.includes(relationshipType, mappingElements);
    if (haveKey && haveAccessibleTarget) {
      return true;
    }
  }
  // Check if combination is valid
  const data = stixCoreRelationshipsMapping[`${fromType}_${toType}`] || [];
  const targetRelations = data.map((r) => r.name);
  return R.includes(relationshipType, targetRelations);
};

export const isRelationBuiltin = (instance: StoreRelation): boolean => {
  if (isInternalRelationship(instance.entity_type) || isStixRefRelationship(instance.entity_type)) {
    return false;
  }
  // Any <-> Any relationship type check
  if (instance.entity_type === RELATION_RELATED_TO) {
    return true;
  }
  if (instance.entity_type === RELATION_REVOKED_BY) {
    return false;
  }
  // Well-define relationship type check
  const definitions = stixCoreRelationshipsMapping[`${instance.fromType}_${instance.toType}`] ?? [];
  // check from toType parent observables
  if (isStixCyberObservable(instance.toType)) {
    definitions.push(...(stixCoreRelationshipsMapping[`${instance.fromType}_${ABSTRACT_STIX_CYBER_OBSERVABLE}`] ?? []));
  }
  // check from fromType parent observables
  if (isStixCyberObservable(instance.fromType)) {
    definitions.push(...(stixCoreRelationshipsMapping[`${ABSTRACT_STIX_CYBER_OBSERVABLE}_${instance.toType}`] ?? []));
  }
  // If definition found, check if the relation is build int
  if (definitions) {
    const rel = definitions.find((d) => d.name === instance.entity_type);
    if (rel) {
      return rel.type === REL_BUILT_IN;
    }
  }
  // Definition not found, throw exception
  const meta = { from: instance.fromType, type: instance.entity_type, to: instance.toType };
  throw UnsupportedError('[STIX] Missing relation definition', meta);
};

export const checkRelationshipRef = (fromType: string, toType: string, relationshipType: string) => {
  const relationRefs = schemaRelationsRefDefinition.getRelationsRef(fromType).filter((rel) => rel.databaseName === relationshipType);
  if (relationRefs.length === 0) {
    throw FunctionalError('The relationship is not allowed', { type: relationshipType, from: fromType, to: toType });
  }
  if (relationRefs.length > 1) {
    throw FunctionalError('Invalid relationship schema', { type: relationshipType, from: fromType, to: toType, data: relationRefs });
  }

  if (!relationRefs[0].isRefExistingForTypes(fromType, toType)) {
    throw FunctionalError('The relationship is not allowed', { type: relationshipType, from: fromType, to: toType });
  }
};
